package net.sppan.blog.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import net.sppan.blog.commons.Constants;
import net.sppan.blog.model.base.BaseRes;
import net.sppan.blog.model.base.Condition;
import net.sppan.blog.model.base.Operators;
import net.sppan.blog.vo.ZtreeView;

import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Res extends BaseRes<Res> implements Serializable {
	public static final Res dao = new Res();

	/**
	 * 获取角色树形菜单
	 * @return
	 */
	public List<Res> getMenuTreeView() {
		return CacheKit.get(Constants.CacheName.menu.get(), "getMenuTreeView", new IDataLoader() {
			@Override
			public List<Res> load() {
				List<Res> list = new ArrayList<Res>();
				LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
				orderby.put("seq", "asc");
				Set<Condition> conditions = new HashSet<Condition>();
				conditions.add(new Condition("pid", Operators.EQ, ""));
				List<Res> allTopMenuList = getList(1, 10000, conditions, orderby);
				for (Res top : allTopMenuList) {
					top.setName("　" + top.getName());
					list.add(top);
					conditions.clear();
					conditions.add(new Condition("pid", Operators.EQ, top.getId()));
					List<Res> allOneMenuList = getList(1, 10000, conditions, orderby);
					for (Res one : allOneMenuList) {
						one.setName("　　" + one.getName());
						list.add(one);
						conditions.clear();
						conditions.add(new Condition("pid", Operators.EQ, one.getId()));
						List<Res> allTwoMenuList = getList(1, 10000, conditions, orderby);
						for (Res two : allTwoMenuList) {
							two.setName("　　　　" + two.getName());
							list.add(two);
						}
					}
				}
				return list;
			}
		});

	}

	/**
	 * 获取导航菜单
	 * @return
	 */
	public List<Res> getMenuAll() {
		List<Res> list = new ArrayList<Res>();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("seq", "asc");
		Set<Condition> conditions = new HashSet<Condition>();
		conditions.add(new Condition("pid", Operators.EQ, ""));
		List<Res> allTopMenuList = this.getList(1, 10000, conditions, orderby);
		for (Res top : allTopMenuList) {
			list.add(top);
			conditions.clear();
			conditions.add(new Condition("pid", Operators.EQ, top.getId()));
			List<Res> allOneMenuList = this.getList(1, 10000, conditions, orderby);
			for (Res one : allOneMenuList) {
				one.setName(one.getName());
				list.add(one);
			}
		}
		return list;
	}

	/**
	 * 获取ZTree权限树
	 * @param type
	 * @param roleId
	 * @return
	 */
	public List<ZtreeView> getZtreeViewList(Integer type, Integer roleId) {
		List<Res> curMenulist = getMenus(roleId);
		List<ZtreeView> ztreeViews = new ArrayList<ZtreeView>();
		ztreeViews.add(new ZtreeView(10000, null, "资源列表", true));
		for (Res res : curMenulist) {
			ZtreeView ztreeView = new ZtreeView();
			ztreeView.setId(res.getInt("id"));
			ztreeView.setName(res.getStr("name"));
			ztreeView.setOpen(true);
			if (res.getInt("pid") == null) {
				ztreeView.setpId(10000);
			} else {
				ztreeView.setpId(res.getInt("pid"));
			}
			ztreeView.setChecked(res.getLong("selected") == 1 ? true : false);
			ztreeViews.add(ztreeView);
		}
		return ztreeViews;
	}

	/**
	 * 获取角色的菜单信息
	 * @param roleId
	 * @return
	 */
	public List<Res> getMenus(Integer roleId) {
		Role role = Role.dao.findById(roleId);
		if (role != null) {
			List<Res> list = Res.dao.find("SELECT *,(CASE WHEN re.id IN (SELECT rr.res_id from sys_role_res rr WHERE rr.role_id=" + roleId + " ) THEN 1 ELSE 0 END) as selected FROM sys_res re where re.enabled=1");
			return list;
		}
		return new ArrayList<>();
	}

	/**
	 * 获取用户的所有资源
	 * @param uid 用户ID
	 * @param type 1菜单|2按钮
	 * @return
	 */
	public Set<String> getUserAllResKeySet(Integer uid) {
		Set<String> sets = CacheKit.get(Constants.CacheName.defaultCache.get(), "getUserAllResourceKeySet_" + uid);
		if (sets == null) {
			List<Res> list = getResourceList(uid, null);
			sets = new HashSet<String>();
			if (list != null) {
				for (Res item : list) {
					sets.add(item.getKey());
				}
			}
			CacheKit.put(Constants.CacheName.defaultCache.get(), "getUserAllResourceKeySet_" + uid, sets);
		}
		return sets;
	}

	/**
	 * 获取用户的所有资源
	 * @param uid 用户ID
	 * @param type 1菜单|2按钮
	 * @return
	 */
	public List<Res> getResourceList(int uid, Integer type) {
		List<Res> resList = CacheKit.get(Constants.CacheName.defaultCache.get(), "getResourceList_" + uid + "_" + type);
		if (resList == null || resList.size() == 0) {
			List<UserRole> userRoles = UserRole.dao.findByUserId(uid);
			if (userRoles == null || userRoles.size() == 0) {
				return null;
			}
			StringBuffer roleIds = new StringBuffer();
			for (UserRole userRole : userRoles) {
				roleIds.append(userRole.getRoleId()).append(",");
			}
			roleIds.deleteCharAt(roleIds.length() - 1);
			if (type != null) {
				resList = this.find("select * from sys_res where id in(select res_id from sys_role_res where role_id in(" + roleIds.toString() + ")) and type=" + type + " and enabled=1  order by seq asc");
			} else {
				resList = this.find("select * from sys_res where id in(select res_id from sys_role_res where role_id in(" + roleIds.toString() + ")) and enabled=1 order by seq asc");
			}
			CacheKit.put(Constants.CacheName.defaultCache.get(), "getResourceList_" + uid + "_" + type, resList);
		}
		return resList;
	}
}
